package com.hctforgreen.greenservice.ui.widget;


import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hctforgreen.greenservice.R;

/**自定义两种弹框，题目，内容
 * 第一种：确定 取消 两个按钮
 * 第二种：确定 一个按钮
 * Created by 390697 on 2016/11/17.
 */
public class MyCustomDialog {

    public static final int YES = 1;
    public static final int NO = 0;
    private Dialog dlg;

    public interface OnAlertSelected {
        void onClick(int whichButton);
    }

    public MyCustomDialog() {
    }

    //该种样式的弹框有标题、内容、取消、确定按键
    public  Dialog showAlert(Context context, String title, String content, String confirm, String cancel, final OnAlertSelected alertDo){
       dlg = new Dialog(context,R.style.BLTheme_Dialog);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.my_custom_dialog_layout,null);
        final int cFullWidth = 1000;
        layout.setMinimumWidth(cFullWidth);

        TextView titleTv = (TextView) layout.findViewById(R.id.dialog_title_tv);
        TextView contentTv = (TextView) layout.findViewById(R.id.dialog_content_tv);
        Button confirmBtn = (Button)layout.findViewById(R.id.dialog_confirm_btn);
        Button cancelBtn = (Button)layout.findViewById(R.id.dialog_cancel_btn);

        if (!TextUtils.isEmpty(title)) {
            titleTv.setText(title);
        }
        if (!TextUtils.isEmpty(content)){
            contentTv.setText(content);
        }
        if (!TextUtils.isEmpty(confirm)){
            confirmBtn.setText(confirm);
        }
        if (!TextUtils.isEmpty(cancel)){
            cancelBtn.setText(cancel);
        }

        confirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dlg.dismiss();
                alertDo.onClick(YES);
            }
        });

        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDo.onClick(NO);
                dlg.dismiss();
            }
        });


        Window w = dlg.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.x = 0;
        lp.gravity = Gravity.CENTER;

        dlg.setContentView(layout);
        dlg.setCancelable(false);
        dlg.setCanceledOnTouchOutside(false);
        //当前窗口属性发生变化时调用
        dlg.onWindowAttributesChanged(lp);
        dlg.show();
        return dlg;
    }



    public  Dialog showSingleButtonAlert(Context context,String title,String content,String btnStr,final OnAlertSelected alertDo){
        final Dialog dlg = new Dialog(context,R.style.BLTheme_Dialog);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.my_custom_dialog_single_button,null);
        final int fullWidth = 1000;
        linearLayout.setMinimumWidth(fullWidth);

        TextView titleTv = (TextView) linearLayout.findViewById(R.id.single_dialog_title);
        TextView contentTv = (TextView) linearLayout.findViewById(R.id.single_dialog_content);
        Button btn = (Button)linearLayout.findViewById(R.id.single_dialog_btn);

        if (!TextUtils.isEmpty(title)){
            titleTv.setText(title);
        }
        if (!TextUtils.isEmpty(content)){
            contentTv.setText(content);
        }
        if (!TextUtils.isEmpty(btnStr)){
            btn.setText(btnStr);
        }

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dlg.dismiss();
                alertDo.onClick(YES);
            }
        });


        Window window = dlg.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.x = 0;

        dlg.setContentView(linearLayout);
        dlg.setCanceledOnTouchOutside(false);
        dlg.setCancelable(false);
        dlg.onWindowAttributesChanged(lp);
        dlg.show();
        return dlg;
    }

    public void destroyDialog(){
        if (dlg != null){
            dlg.dismiss();
        }
    }
}
